package com.cn.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.cn.dao.ProductDao;
import com.cn.dao.impl.ProductDaoImpl;
import com.cn.domain.PageBean;
import com.cn.domain.Product;
import com.cn.service.ProductService;
import com.cn.utils.JdbcUtils;

public class ProductServiceImpl implements ProductService {

	ProductDao pd = new ProductDaoImpl();
	
	public void addProduct(Product p) {
		
		JdbcUtils.getConnectionWithTransaction();
		try {
			pd.saveProduct(p);
			JdbcUtils.commit();
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
		//throw new RuntimeException("测试异常--------");
	}

	public List<Product> conditionList(Product p) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			List<Product> list = pd.getListByCondition(p);
			JdbcUtils.commit();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
	}

	public void deleteProduct(Product p) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			pd.deleteProduct(p);
			JdbcUtils.commit();
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}

		
	}

	public Product getProductById(Integer id) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			Product product = pd.getProductById(id);
			JdbcUtils.commit();
			return product;
		} catch (Exception e) {
			JdbcUtils.rollback();
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	public void updateProduct(Product p) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			pd.updateProduct(p);
			JdbcUtils.commit();
		} catch (Exception e) {
			JdbcUtils.rollback();
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		
	}

	public PageBean getProductList(String currentPage, String key, String category) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			int totalCount = pd.getTotalCount(key, category);
			System.out.println(totalCount);
			PageBean pageBean = new PageBean(totalCount,currentPage);
			List<Product> list = pd.getProductList(key, category, pageBean.getStart(), pageBean.getPerCount());
			pageBean.setList(list);
			JdbcUtils.commit();
			return pageBean;
		} catch (Exception e) {
			JdbcUtils.rollback();
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	public List<Product> getProductByTipName(String tipname) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			List<Product> list = pd.getProductByTipName(tipname);
			JdbcUtils.commit();
			return list;
		} catch (Exception e) {
			JdbcUtils.rollback();
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	public List<Product> getTopSaleList(String year, String mouth) {
		JdbcUtils.getConnectionWithTransaction();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		if(year==null||"".equals(year)||mouth==null||"".equals(mouth)){
			pd.getTopSaleList(null, null);
		}
		Date date1 = null;
		try {
			date1 = sdf.parse(year+mouth);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date1);
		Date date2 = null;
		if(Integer.parseInt(mouth) == 12){
			try {
				date2 = sdf.parse(Integer.parseInt(year)+1+"01");
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			date2 = sdf.parse(year+(Integer.parseInt(mouth)+1));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date2);
		List<Product> list = pd.getTopSaleList(date1, date2);
		JdbcUtils.commit();
		return list;
	}

}
